package crowMessenger;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;

public class BuddyList extends JList<Buddy>{
	private static final long serialVersionUID = -9171567074287423256L;
	private DefaultListModel<Buddy> model;
	private ArrayList<Buddy> buddyList = new ArrayList<Buddy>();//contains all buddys painted here
	private ArrayList<Roster> rosterList = new ArrayList<Roster>();//All rosters of all accounts to create buddylists
	private ArrayList<XMPPConnection> connectionList = new ArrayList<XMPPConnection>();//all enabled connections
	private ChatFrame MainChatFrame; //would be called if no chat window was already opened

	protected MainPanel con;

	public BuddyList(MainPanel con){
		this.con = con;
		model = new DefaultListModel<Buddy>();
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // single selection
		setFixedCellHeight(40);
		setCellRenderer(new ListCellRenderer<Buddy>() {
			@Override
			public Component getListCellRendererComponent(
					JList<? extends Buddy> list, Buddy value, int index,
					boolean isSelected, boolean cellHasFocus) {
				// TODO Auto-generated method stub
				if(isSelected)
					value.setBackground(Color.red);
				else
					value.setBackground(Color.gray);
				return value;
			}
		});

		addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2){
					System.out.println(StaticMethods.openChats());
					if(StaticMethods.openChats() == 0){
						MainChatFrame = new ChatFrame(getSelectedValue());
						StaticMethods.increaseOpenChats();
					}
					else{
						MainChatFrame.getChatTabs().addNewChat(getSelectedValue());
						StaticMethods.increaseOpenChats();
					}
				}
			}
		});
		setModel(model);
	}

	/* adds the roster to the arraylist of rosters 
	 * adds the connection to the arraylist of connections
	 * adds the chatmanager to the arraylist of chatmanagers
	 * gets the roster entries in the roster and add them to the arraylist of buddies and paint them to MainPanel */
	protected void addRosterConnection(Roster roster,XMPPConnection connection,Account acc){
		rosterList.add(roster);
		connectionList.add(connection);
		Collection<RosterEntry> entries;
		entries = roster.getEntries();
		for (RosterEntry entry : entries) {
			if(entry.getUser().endsWith("gmail.com")){
				System.out.println(entry+"/"+entry.getName()+"/"+entry.getUser()+"/"+entry.getStatus()+"/"+entry.getType());
				buddyList.add(new Buddy(entry, Color.gray,acc,connection));
			}
		}
		//initial painting after login
		for(Buddy b : buddyList){
			model.addElement(b);
		}
	}
}
